apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../swiper"],function(V,$api,$,app,service,Swiper){
  var el = document.querySelector('.container');
  var vm = {
    el: el,
    data: {
      showAllNav: false,
      catId: '', // 二级分类id,用户获取下级分类
      cat_id: '', // 二级\三级分类id,用于获取商品
      classfiyList: [], // 分类列表
      goodsList: [], // 商品列表
      title: '标题',
      page: 1,
      hasMoreData: true,
      userInfo: $api.getStorage("user").data, // 获取用户身份
    },
    created: function () {
      var that = this;
      this.catId = api.pageParam.cat_id;
      this.cat_id = api.pageParam.cat_id;
      var title = api.pageParam.name;
      document.querySelector('.wrapper .title').innerText = title;
      this.init();

      // 触底
      api.addEventListener({
          name: 'scrolltobottom',
          extra:{
              threshold:0
          }
      }, function(ret, err){
          that.scrolltobottom()
      });

      // 下拉刷新
      app.refreshHeaderInfo(function () {
        that.page = 1;
        that.goodsList = [];
        that.getGoodsList();
      });
    },
    methods: {
      showNav: function () {
        this.showAllNav = true;
      },
      hideNav: function () {
        this.showAllNav = false;
      },
      // 切换分类
      switchNav: function (index, id) {
        $('.no-goods').hide();
        var nav = document.querySelectorAll('.top-nav li');
        var navAll = document.querySelectorAll('.top-nav-all li');
        for (var i = 0, len = nav.length; i < len; i++) {
          nav[i].classList.remove('select-nav');
          navAll[i].classList.remove('select-nav');
        }
        if (index != -1) {
          nav[index+1].classList.add('select-nav');
          navAll[index+1].classList.add('select-nav');
        } else {
          nav[0].classList.add('select-nav');
          navAll[0].classList.add('select-nav');
        }

        var cat_id;
        if (id) {
          this.cat_id = id;
        } else {
          this.cat_id = this.catId;
        }
        this.page = 1;
        this.goodsList = [];
        this.getGoodsList();
      },

      init: function () {
        var that = this;
        this.getNav(function () {
          that.getGoodsList();
        });
      },

      // 获取分类
      getNav: function (callback) {
        var that = this;
        app.ajax(
          service['homeGetSysCategory'],
          {
            data: {
              values: {
                cat_id: that.catId,
              }
            }
          },
          function (res) {
            if (res.code == 0) {
              that.classfiyList = res.data;
              callback();
            } else {
              api.toast({
                  msg: res.message,
                  duration: 2000,
                  location: 'bottom'
              });
            }
          }
        );
      },

      // 获取商品列表
      getGoodsList: function () {
        var that = this;
        app.ajax(
          service['getSysCategoryGoodsList'],
          {
            data: {
              values: {
                cat_id: that.cat_id,
                page: that.page
              }
            }
          },
          function (res) {
            if (res.code == 0) {
              if (res.data.length > 0) {
                for (var i = 0, len = res.data.length; i < len; i++) {
                  that.goodsList.push(res.data[i]);
                }
                that.hasMoreData = true;
              } else {
                that.hasMoreData = false;
                if (that.page == 1) {
                  $('.no-goods').show();
                }
                api.toast({
                    msg: '没有跟多数据了',
                    duration: 2000,
                    location: 'bottom'
                });

              }
            } else {
              api.toast({
                  msg: res.message,
                  duration: 2000,
                  location: 'bottom'
              });
            }
          }
        );
      },

      // 前往商品详情
      toGoodsDetail: function (goodsId, isGroup) {
        if (isGroup) {
          // 拼团商品
          app.openWin('groupBuyDetail', {goodsId: goodsId});
        } else {
          // 普通商品
          app.openWin('buyGoods', {goodsId: goodsId});
        }
      },

      // 加载更多
      scrolltobottom: function () {
        if (this.hasMoreData) {
          this.page += 1;
          this.getGoodsList();
        }
      }
    }
  };
  var even = function() {}
  var newVue = new Vue(vm);
  even();
});
}
